<?php

/**
 * uniBoard ACP Blacklist New User Names Form
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_new_blist_name{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * This is list of all-right names we are going to blacklist
	 *
	 * @var array
	 */
	
	private $names		 	= array();
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
			
		// Update?
		if ( $appCore -> _POST( 'update') == 1)
		{
			// Errors
			$errors = array();
			
			// Grab Names to disallow
			$names = explode( "\n", $appCore -> _POST('blist_names'));
			
			// First Cycle - reiterate names. Empty ones are pwt
			$names_n = count( $names);
			
			// Loop
			for ( $i = 0; $i < $names_n; $i ++)
			{
				// Get Name
				$name = trim( $names[$i]);
				
				// Name not empty?
				if ( isset( $name[0]))
				{
					// Pass Name to ready array
					$this -> names[] = $name;
				}
			}
			
			// Api All-rite?
			if ( empty( $this -> names))
			{
				// Draw Errors
				$appCore -> output -> error( $appCore -> lang['acp_disallow_names_empty'], $appCore -> lang['acp_disallow_names']);
				
				// Repeat Form
				$this -> drawForm();
			}
			else
			{
				// Set Log
				$appCore -> logAdmin( 'log_admin_blisted_user_names', htmlspecialchars( join( ', ', $this -> names)));
				
				// Insert SQL
				$insert_sql = array();
				
				// Reiterate our Names
				$names_n = count( $this -> names);
				
				// Loop
				for ( $i = 0; $i < $names_n; $i ++)
				{
					// Add User to MassInsert
					$insert_sql[] = '\'' . $appCore -> db -> strEscape( $this -> names[$i]) . '\'';
				}
				
				// Execute Multiple Insert
				$appCore -> db -> multipleInsert( 'disallow_name', $insert_sql, 'disallow_names');
				
				// Flush Cache
				class_cache::flushCache( 'disallows');
				
				// Redirect us back to list
				$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=blist_names'), $appCore -> lang['acp_blist_name_saved']);
			}
		}
		else
		{
			// Draw Form
			$this -> drawForm();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Form
	 *
	 */
	
	function drawForm(){
	
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
				
		// Cache output
		ob_start();
				
		// Open Form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=new_blist_name'));
		echo $this -> appCore -> drawHidden( 'update', 1);
		echo $this -> appCore -> output -> openFormBlock();
		
		// Draw First Field
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_blacklist_form_names'], $this-> appCore -> drawTextArea( 'blist_names', join( "\n", $this -> names)), $this -> appCore -> lang['acp_blacklist_form_expl']);
		
		// Close Form
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['new_blacklist_name_form_submit']);
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> closeForm();
		
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['new_blacklist_name_form'], array(
			$this -> appCore -> adminLink( 'act=blist_names') => $this -> appCore -> lang['acp_disallow_names'],
			'' => $this -> appCore -> lang['new_blacklist_name_form']
		), ob_get_clean());
			
	}

}